Load all required libraries.
library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.3
## -- Attaching packages ------------------------------------------------------------------------ tidyverse 1.3.0 --
## v ggplot2 3.3.2 v purrr 0.3.4
## v tibble 3.0.3 v dplyr 1.0.0
## v tidyr 1.1.0 v stringr 1.4.0
## v readr 1.3.1 v forcats 0.5.0
## Warning: package 'ggplot2' was built under R version 3.6.3
## Warning: package 'tibble' was built under R version 3.6.3
## Warning: package 'readr' was built under R version 3.6.3
## Warning: package 'dplyr' was built under R version 3.6.3
## Warning: package 'forcats' was built under R version 3.6.3
## -- Conflicts --------------------------------------------------------------------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(plotly)
## Warning: package 'plotly' was built under R version 3.6.3
##
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
library(broom)
## Warning: package 'broom' was built under R version 3.6.3
Read in raw data from RDS.
raw_data <- readRDS("./n1_n2_cleaned_cases.rds")
Make a few small modifications to names and data for visualizations.
final_data <- raw_data %>% mutate(log_copy_per_L = log10(mean_copy_num_L)) %>%
rename(Facility = wrf) %>%
mutate(Facility = recode(Facility,
"NO" = "WRF A",
"MI" = "WRF B",
"CC" = "WRF C"))
Seperate the data by gene target to ease layering in the final plot
#make three data layers
only_positives <<- subset(final_data, (!is.na(final_data$Facility)))
only_n1 <- subset(only_positives, target == "N1")
only_n2 <- subset(only_positives, target == "N2")
only_background <<-final_data %>%
select(c(date, cases_cum_clarke, new_cases_clarke, X7_day_ave_clarke, cases_per_100000_clarke)) %>%
group_by(date) %>% summarise_if(is.numeric, mean)
#specify fun colors
background_color <- "#7570B3"
seven_day_ave_color <- "#E6AB02"
marker_colors <- c("N1" = '#1B9E77',"N2" ='#D95F02')
#remove facilty C for now
#only_n1 <- only_n1[!(only_n1$Facility == "WRF C"),]
#only_n2 <- only_n2[!(only_n2$Facility == "WRF C"),]
only_n1 <- only_n1[!(only_n1$Facility == "WRF A" & only_n1$date == "2020-11-02"), ]
only_n2 <- only_n2[!(only_n2$Facility == "WRF A" & only_n2$date == "2020-11-02"), ]
Build the main plot
#first layer is the background epidemic curve
p1 <- only_background %>%
plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~new_cases_clarke,
type = "bar",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Daily Cases: ', new_cases_clarke),
alpha = 0.5,
name = "Daily Reported Cases",
color = background_color,
colors = background_color,
showlegend = FALSE) %>%
layout(yaxis = list(title = "Clarke County Daily Cases", showline=TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#renders the main plot layer two as seven day moving average
p1 <- p1 %>% plotly::add_trace(x = ~date, y = ~X7_day_ave_clarke,
type = "scatter",
mode = "lines",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Seven-Day Moving Average: ', X7_day_ave_clarke),
name = "Seven Day Moving Average Athens",
line = list(color = seven_day_ave_color),
showlegend = FALSE)
#renders the main plot layer three as positive target hits
p2 <- plotly::plot_ly() %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n1,
symbol = ~Facility,
marker = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
plotly::add_trace(x = ~date, y = ~mean_copy_num_L,
type = "scatter",
mode = "markers",
hoverinfo = "text",
text = ~paste('</br> Date: ', date,
'</br> Facility: ', Facility,
'</br> Target: ', target,
'</br> Copies/L: ', round(mean_copy_num_L, digits = 2)),
data = only_n2,
symbol = ~Facility,
marker = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(yaxis = list(title = "SARS CoV-2 Copies/L",
showline = TRUE,
type = "log",
dtick = 1,
automargin = TRUE)) %>%
layout(legend = list(orientation = "h", x = 0.2, y = -0.3))
#adds the limit of detection dashed line
p2 <- p2 %>% plotly::add_segments(x = as.Date("2020-03-14"),
xend = ~max(date + 10),
y = 3571.429, yend = 3571.429,
opacity = 0.35,
line = list(color = "black", dash = "dash")) %>%
layout(annotations = list(x = as.Date("2020-03-28"), y = 3.8, xref = "x", yref = "y",
text = "Limit of Detection", showarrow = FALSE))
p1
## Warning: `arrange_()` is deprecated as of dplyr 0.7.0.
## Please use `arrange()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
## Warning: Ignoring 1 observations
p2
## Warning: `group_by_()` is deprecated as of dplyr 0.7.0.
## Please use `group_by()` instead.
## See vignette('programming') for more help
## This warning is displayed once every 8 hours.
## Call `lifecycle::last_warnings()` to see where this warning was generated.
Combine the two main plot pieces as a subplot
#seperate n1 and n2 frames by site
#n1
wrf_a_only_n1 <- subset(only_n1, Facility == "WRF A")
wrf_b_only_n1 <- subset(only_n1, Facility == "WRF B")
wrf_c_only_n1 <- subset(only_n1, Facility == "WRF C")
#n2
wrf_a_only_n2 <- subset(only_n2, Facility == "WRF A")
wrf_b_only_n2 <- subset(only_n2, Facility == "WRF B")
wrf_c_only_n2 <- subset(only_n2, Facility == "WRF C")
#rejoin the old data frames then seperate in to averages for each plant.
wrfa_both <- full_join(wrf_a_only_n1, wrf_a_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "cases_cum_clarke", "new_cases_clarke", "X7_day_ave_clarke", "cases_per_100000_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfb_both <- full_join(wrf_b_only_n1, wrf_b_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "cases_cum_clarke", "new_cases_clarke", "X7_day_ave_clarke", "cases_per_100000_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
wrfc_both <- full_join(wrf_c_only_n1, wrf_c_only_n2)%>%
select(c(date, mean_total_copies)) %>%
group_by(date) %>%
summarize_if(is.numeric, mean) %>%
ungroup() %>%
mutate(log_total_copies_both = log10(mean_total_copies))
## Joining, by = c("date", "cases_cum_clarke", "new_cases_clarke", "X7_day_ave_clarke", "cases_per_100000_clarke", "Facility", "collection_num", "target", "mean_copy_num_uL_rxn", "mean_copy_num_L", "sd_L", "mean_total_copies", "sd_total_copies", "log_copy_per_L")
#get max date
maxdate <- max(wrfa_both$date)
mindate <- min(wrfa_both$date)
Build loess smoothing figures figures
This makes the individual plots
#**************************************WRF A PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_botha <- ggplot(wrfa_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_botha<<-..y..), method = "loess", color = '#1B9E77',
span = 0.6, n = 282)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_botha
## `geom_smooth()` using formula 'y ~ x'
fit_botha
## [1] 12.90329 12.90613 12.90897 12.91182 12.91465 12.91747 12.92025 12.92300
## [9] 12.92569 12.92833 12.93089 12.93337 12.93577 12.93806 12.94024 12.94230
## [17] 12.94423 12.94602 12.94766 12.94914 12.95044 12.95157 12.95254 12.95338
## [25] 12.95410 12.95471 12.95521 12.95561 12.95591 12.95611 12.95623 12.95626
## [33] 12.95621 12.95609 12.95591 12.95566 12.95535 12.95499 12.95458 12.95414
## [41] 12.95365 12.95313 12.95259 12.95202 12.95144 12.95085 12.95025 12.94965
## [49] 12.94906 12.94847 12.94778 12.94686 12.94574 12.94440 12.94287 12.94114
## [57] 12.93924 12.93715 12.93490 12.93249 12.92993 12.92722 12.92437 12.92139
## [65] 12.91829 12.91507 12.91175 12.90833 12.90481 12.90121 12.89754 12.89379
## [73] 12.88999 12.88612 12.88222 12.87827 12.87429 12.87029 12.86627 12.86225
## [81] 12.85822 12.85420 12.85020 12.84621 12.84226 12.83835 12.83447 12.83065
## [89] 12.82690 12.82321 12.81959 12.81606 12.81261 12.80927 12.80603 12.80291
## [97] 12.79990 12.79703 12.79429 12.79116 12.78716 12.78236 12.77686 12.77073
## [105] 12.76404 12.75688 12.74934 12.74148 12.73338 12.72514 12.71682 12.70851
## [113] 12.70029 12.69223 12.68442 12.67694 12.66986 12.66327 12.65725 12.65187
## [121] 12.64722 12.64338 12.64041 12.63842 12.63747 12.63764 12.63902 12.64169
## [129] 12.64614 12.65264 12.66093 12.67071 12.68173 12.69370 12.70636 12.71942
## [137] 12.73261 12.74566 12.75829 12.77023 12.78120 12.79093 12.80058 12.81143
## [145] 12.82337 12.83632 12.85017 12.86483 12.88021 12.89620 12.91272 12.92966
## [153] 12.94693 12.96443 12.98207 12.99976 13.01738 13.03486 13.05209 13.06897
## [161] 13.08541 13.10132 13.11660 13.13114 13.14487 13.15767 13.16945 13.18013
## [169] 13.19190 13.20681 13.22447 13.24451 13.26655 13.29020 13.31509 13.34083
## [177] 13.36706 13.39338 13.41943 13.44482 13.46917 13.49210 13.51324 13.53220
## [185] 13.54861 13.56209 13.57225 13.57872 13.58113 13.58046 13.57809 13.57412
## [193] 13.56870 13.56196 13.55402 13.54501 13.53507 13.52432 13.51289 13.50092
## [201] 13.48854 13.47587 13.46304 13.45019 13.43744 13.42362 13.40757 13.38944
## [209] 13.36937 13.34752 13.32403 13.29906 13.27276 13.24527 13.21675 13.18735
## [217] 13.15721 13.12648 13.09532 13.06387 13.03229 13.00072 12.96931 12.93822
## [225] 12.90760 12.87758 12.84833 12.81999 12.79272 12.76666 12.74196 12.71735
## [233] 12.69158 12.66485 12.63734 12.60924 12.58075 12.55205 12.52334 12.49480
## [241] 12.46661 12.43899 12.41210 12.38614 12.36131 12.33690 12.31212 12.28706
## [249] 12.26178 12.23637 12.21088 12.18541 12.16001 12.13476 12.10974 12.08501
## [257] 12.06066 12.03675 12.01336 11.99037 11.96763 11.94512 11.92285 11.90080
## [265] 11.87897 11.85735 11.83594 11.81474 11.79373 11.77290 11.75227 11.73181
## [273] 11.71152 11.69140 11.67145 11.65165 11.63199 11.61248 11.59311 11.57387
## [281] 11.55476 11.53577
#assign fits to a vector
both_trenda <- fit_botha
#extract y min and max for each
limits_botha <- ggplot_build(extract_botha)$data
## `geom_smooth()` using formula 'y ~ x'
limits_botha <- as.data.frame(limits_botha)
both_ymina <- limits_botha$ymin
both_ymaxa <- limits_botha$ymax
#reassign dataframes (just to be safe)
work_botha <- wrfa_both
#fill in missing dates to smooth fits
work_botha <- work_botha %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_botha <- work_botha$date
#create a new smooth dataframe to layer
smooth_frame_botha <- data.frame(date_vec_botha, both_trenda, both_ymina, both_ymaxa)
#WRF A
#plot smooth frames
p_wrf_a <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_botha, y = ~both_trenda,
data = smooth_frame_botha,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha,
'</br> Median Log Copies: ', round(both_trenda, digits = 2)),
line = list(color = '#1B9E77', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_botha, ymin = ~both_ymina, ymax = ~both_ymaxa,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_botha, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxa, digits = 2),
'</br> Min Log Copies: ', round(both_ymina, digits = 2)),
name = "",
fillcolor = '#1B9E77',
line = list(color = '#1B9E77')) %>%
layout(yaxis = list(title = "Total Log SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF A") %>%
plotly::add_segments(x = as.Date("2020-06-24"),
xend = as.Date("2020-06-24"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "Bars Repoen",
hoverinfo = "text",
text = "</br> Bars Reopen",
"</br> 2020-06-24",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-07-09"),
xend = as.Date("2020-07-09"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "Mask Mandate",
hoverinfo = "text",
text = "</br> Mask Mandate",
"</br> 2020-07-09",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-08-20"),
xend = as.Date("2020-08-20"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "</br> Classes Begin",
"</br> 2020-08-20",
hoverinfo = "text",
text = "Classes Begin",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-10-03"),
xend = as.Date("2020-10-03"),
y = ~min(both_ymina), yend = ~max(both_ymaxa),
opacity = 0.35,
name = "</br> First Home Football Game",
"</br> 2020-10-03",
hoverinfo = "text",
text = "First Home Football Game",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfa_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#1B9E77', size = 6, opacity = 0.65))
p_wrf_a
save(p_wrf_a, file = "./plotly_objs/p_wrf_a.rda")
#**************************************WRF B PLOT**********************************************
#add trendlines
#extract data from geom_smooth
#both extract
# *********************************span 0.6***********************************
#*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothb <- ggplot(wrfb_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothb<<-..y..), method = "loess", color = '#D95F02',
span = 0.6, n = 282)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothb
## `geom_smooth()` using formula 'y ~ x'
fit_bothb
## [1] 12.55489 12.55384 12.55286 12.55193 12.55106 12.55024 12.54946 12.54873
## [9] 12.54804 12.54738 12.54676 12.54617 12.54560 12.54506 12.54453 12.54402
## [17] 12.54353 12.54304 12.54256 12.54209 12.54161 12.54113 12.54064 12.54015
## [25] 12.53965 12.53916 12.53867 12.53818 12.53770 12.53723 12.53677 12.53633
## [33] 12.53590 12.53549 12.53510 12.53473 12.53439 12.53408 12.53379 12.53354
## [41] 12.53332 12.53314 12.53300 12.53290 12.53284 12.53282 12.53286 12.53294
## [49] 12.53308 12.53327 12.53343 12.53351 12.53350 12.53342 12.53328 12.53310
## [57] 12.53287 12.53262 12.53236 12.53209 12.53183 12.53158 12.53137 12.53120
## [65] 12.53108 12.53102 12.53104 12.53115 12.53135 12.53166 12.53208 12.53264
## [73] 12.53335 12.53420 12.53522 12.53641 12.53779 12.53937 12.54097 12.54241
## [81] 12.54371 12.54489 12.54596 12.54693 12.54783 12.54867 12.54946 12.55023
## [89] 12.55098 12.55174 12.55252 12.55333 12.55419 12.55513 12.55614 12.55726
## [97] 12.55849 12.55986 12.56137 12.56305 12.56491 12.56696 12.56923 12.57173
## [105] 12.57447 12.57747 12.57975 12.58043 12.57970 12.57771 12.57464 12.57067
## [113] 12.56595 12.56068 12.55501 12.54912 12.54318 12.53736 12.53184 12.52678
## [121] 12.52236 12.51875 12.51612 12.51464 12.51449 12.51583 12.51884 12.52369
## [129] 12.53089 12.54058 12.55242 12.56606 12.58115 12.59734 12.61428 12.63163
## [137] 12.64902 12.66612 12.68257 12.69802 12.71212 12.72453 12.73718 12.75213
## [145] 12.76919 12.78815 12.80883 12.83105 12.85460 12.87930 12.90495 12.93137
## [153] 12.95835 12.98572 13.01328 13.04084 13.06820 13.09517 13.12157 13.14721
## [161] 13.17188 13.19540 13.21758 13.23823 13.25715 13.27415 13.28905 13.30165
## [169] 13.31382 13.32742 13.34221 13.35799 13.37452 13.39158 13.40894 13.42639
## [177] 13.44369 13.46063 13.47697 13.49250 13.50699 13.52021 13.53195 13.54198
## [185] 13.55007 13.55599 13.55954 13.56047 13.55858 13.55392 13.54688 13.53767
## [193] 13.52652 13.51363 13.49924 13.48355 13.46679 13.44917 13.43092 13.41226
## [201] 13.39339 13.37455 13.35595 13.33782 13.32036 13.30194 13.28090 13.25747
## [209] 13.23183 13.20419 13.17475 13.14373 13.11132 13.07772 13.04315 13.00780
## [217] 12.97189 12.93560 12.89916 12.86276 12.82660 12.79089 12.75584 12.72164
## [225] 12.68851 12.65664 12.62625 12.59753 12.57068 12.54592 12.52345 12.50165
## [233] 12.47894 12.45552 12.43160 12.40739 12.38311 12.35897 12.33517 12.31193
## [241] 12.28947 12.26798 12.24769 12.22881 12.21154 12.19508 12.17854 12.16202
## [249] 12.14561 12.12938 12.11343 12.09786 12.08274 12.06817 12.05424 12.04104
## [257] 12.02865 12.01716 12.00667 11.99704 11.98808 11.97979 11.97217 11.96519
## [265] 11.95886 11.95316 11.94810 11.94366 11.93983 11.93662 11.93400 11.93198
## [273] 11.93055 11.92969 11.92941 11.92970 11.93054 11.93193 11.93386 11.93633
## [281] 11.93934 11.94286
#assign fits to a vector
both_trendb <- fit_bothb
#extract y min and max for each
limits_bothb <- ggplot_build(extract_bothb)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothb <- as.data.frame(limits_bothb)
both_yminb <- limits_bothb$ymin
both_ymaxb <- limits_bothb$ymax
#reassign dataframes (just to be safe)
work_bothb <- wrfb_both
#fill in missing dates to smooth fits
work_bothb <- work_bothb %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothb <- work_bothb$date
#create a new smooth dataframe to layer
smooth_frame_bothb <- data.frame(date_vec_bothb, both_trendb, both_yminb, both_ymaxb)
#WRF B
#plot smooth frames
p_wrf_b <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothb, y = ~both_trendb,
data = smooth_frame_bothb,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb,
'</br> Median Log Copies: ', round(both_trendb, digits = 2)),
line = list(color = '#D95F02', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothb, ymin = ~both_yminb, ymax = ~both_ymaxb,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothb, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxb, digits = 2),
'</br> Min Log Copies: ', round(both_yminb, digits = 2)),
name = "",
fillcolor = '#D95F02',
line = list(color = '#D95F02')) %>%
layout(yaxis = list(title = "Total Log SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF B") %>%
plotly::add_segments(x = as.Date("2020-06-24"),
xend = as.Date("2020-06-24"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "Bars Repoen",
hoverinfo = "text",
text = "</br> Bars Reopen",
"</br> 2020-06-24",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-07-09"),
xend = as.Date("2020-07-09"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "Mask Mandate",
hoverinfo = "text",
text = "</br> Mask Mandate",
"</br> 2020-07-09",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-08-20"),
xend = as.Date("2020-08-20"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "</br> Classes Begin",
"</br> 2020-08-20",
hoverinfo = "text",
text = "Classes Begin",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-10-03"),
xend = as.Date("2020-10-03"),
y = ~min(both_yminb), yend = ~max(both_ymaxb),
opacity = 0.35,
name = "</br> First Home Football Game",
"</br> 2020-10-03",
hoverinfo = "text",
text = "First Home Football Game",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfb_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#D95F02', size = 6, opacity = 0.65))
p_wrf_b
save(p_wrf_b, file = "./plotly_objs/p_wrf_b.rda")
#**************************************WRF C PLOT********************************************** #add trendlines #extract data from geom_smooth # *********************************span 0.6*********************************** #*****************Must always update the n = TOTAL NUMBER OF DAYS*************************
extract_bothc <- ggplot(wrfc_both, aes(x = date, y = log_total_copies_both)) +
stat_smooth(aes(outfit=fit_bothc<<-..y..), method = "loess", color = '#E7298A',
span = 0.6, n = 282)
## Warning: Ignoring unknown aesthetics: outfit
#look at the fits to align dates and total observations
#both
extract_bothc
## `geom_smooth()` using formula 'y ~ x'
fit_bothc
## [1] 11.78676 11.79374 11.80074 11.80773 11.81472 11.82167 11.82858 11.83543
## [9] 11.84220 11.84887 11.85544 11.86188 11.86818 11.87432 11.88029 11.88607
## [17] 11.89165 11.89701 11.90212 11.90699 11.91159 11.91591 11.91992 11.92362
## [25] 11.92699 11.93001 11.93267 11.93495 11.93683 11.93835 11.93955 11.94045
## [33] 11.94107 11.94142 11.94152 11.94139 11.94103 11.94047 11.93973 11.93880
## [41] 11.93773 11.93651 11.93516 11.93371 11.93216 11.93053 11.92884 11.92710
## [49] 11.92534 11.92356 11.92177 11.92001 11.91828 11.91659 11.91497 11.91343
## [57] 11.91198 11.91015 11.90746 11.90394 11.89965 11.89461 11.88887 11.88246
## [65] 11.87543 11.86780 11.85963 11.85095 11.84179 11.83219 11.82221 11.81186
## [73] 11.80119 11.79025 11.77906 11.76766 11.75611 11.74442 11.73265 11.72083
## [81] 11.70900 11.69719 11.68545 11.67382 11.66233 11.65102 11.63993 11.62909
## [89] 11.61856 11.60836 11.59853 11.58912 11.58016 11.57169 11.56375 11.55637
## [97] 11.54960 11.54347 11.53802 11.53330 11.52933 11.52616 11.52383 11.52238
## [105] 11.52183 11.52224 11.52327 11.52460 11.52624 11.52822 11.53056 11.53328
## [113] 11.53642 11.53998 11.54400 11.54849 11.55349 11.55900 11.56507 11.57171
## [121] 11.57893 11.58678 11.59527 11.60441 11.61425 11.62479 11.63607 11.64810
## [129] 11.66298 11.68235 11.70554 11.73190 11.76076 11.79147 11.82336 11.85579
## [137] 11.88808 11.91958 11.94962 11.97756 12.00273 12.02447 12.04540 12.06845
## [145] 12.09345 12.12020 12.14852 12.17822 12.20912 12.24103 12.27376 12.30712
## [153] 12.34094 12.37502 12.40918 12.44323 12.47699 12.51026 12.54287 12.57462
## [161] 12.60534 12.63483 12.66290 12.68938 12.71407 12.73679 12.75735 12.77557
## [169] 12.79294 12.81097 12.82951 12.84844 12.86760 12.88685 12.90604 12.92504
## [177] 12.94370 12.96187 12.97942 12.99619 13.01205 13.02686 13.04046 13.05272
## [185] 13.06349 13.07263 13.07999 13.08544 13.08882 13.09092 13.09257 13.09368
## [193] 13.09417 13.09396 13.09299 13.09116 13.08840 13.08464 13.07979 13.07377
## [201] 13.06651 13.05793 13.04795 13.03649 13.02347 13.00773 12.98835 12.96560
## [209] 12.93976 12.91110 12.87991 12.84647 12.81105 12.77393 12.73539 12.69570
## [217] 12.65515 12.61401 12.57256 12.53109 12.48985 12.44915 12.40925 12.37043
## [225] 12.33296 12.29714 12.26323 12.23152 12.20227 12.17578 12.15232 12.12961
## [233] 12.10542 12.08008 12.05393 12.02729 12.00050 11.97388 11.94778 11.92252
## [241] 11.89843 11.87584 11.85509 11.83651 11.82042 11.80564 11.79082 11.77608
## [249] 11.76153 11.74729 11.73347 11.72019 11.70756 11.69571 11.68474 11.67476
## [257] 11.66591 11.65828 11.65200 11.64692 11.64280 11.63965 11.63744 11.63618
## [265] 11.63585 11.63644 11.63794 11.64035 11.64365 11.64783 11.65290 11.65882
## [273] 11.66561 11.67325 11.68172 11.69102 11.70115 11.71208 11.72382 11.73635
## [281] 11.74966 11.76375
#assign fits to a vector
both_trendc <- fit_bothc
#extract y min and max for each
limits_bothc <- ggplot_build(extract_bothc)$data
## `geom_smooth()` using formula 'y ~ x'
limits_bothc <- as.data.frame(limits_bothc)
both_yminc <- limits_bothc$ymin
both_ymaxc <- limits_bothc$ymax
#reassign dataframes (just to be safe)
work_bothc <- wrfc_both
#fill in missing dates to smooth fits
work_bothc <- work_bothc %>% complete(date = seq(min(date), max(date), by = "1 day"))
date_vec_bothc <- work_bothc$date
#create a new smooth dataframe to layer
smooth_frame_bothc <- data.frame(date_vec_bothc, both_trendc, both_yminc, both_ymaxc)
#WRF C
#plot smooth frames
p_wrf_c <- plotly::plot_ly() %>%
plotly::add_lines(x = ~date_vec_bothc, y = ~both_trendc,
data = smooth_frame_bothc,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc,
'</br> Median Log Copies: ', round(both_trendc, digits = 2)),
line = list(color = '#E7298A', size = 8, opacity = 0.65),
showlegend = FALSE) %>%
layout(xaxis = list(range = c(mindate - 7, maxdate + 7))) %>% #buffer here
plotly::add_ribbons(x ~date_vec_bothc, ymin = ~both_yminc, ymax = ~both_ymaxc,
showlegend = FALSE,
opacity = 0.25,
hoverinfo = "text",
text = ~paste('</br> Date: ', date_vec_bothc, #leaving in case we want to change
'</br> Max Log Copies: ', round(both_ymaxc, digits = 2),
'</br> Min Log Copies: ', round(both_yminc, digits = 2)),
name = "",
fillcolor = '#E7298A',
line = list(color = '#E7298A')) %>%
layout(yaxis = list(title = "Total Log SARS CoV-2 Copies",
showline = TRUE,
automargin = TRUE)) %>%
layout(xaxis = list(title = "Date")) %>%
layout(title = "WRF C") %>%
plotly::add_segments(x = as.Date("2020-06-24"),
xend = as.Date("2020-06-24"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "Bars Repoen",
hoverinfo = "text",
text = "</br> Bars Reopen",
"</br> 2020-06-24",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-07-09"),
xend = as.Date("2020-07-09"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "Mask Mandate",
hoverinfo = "text",
text = "</br> Mask Mandate",
"</br> 2020-07-09",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-08-20"),
xend = as.Date("2020-08-20"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "</br> Classes Begin",
"</br> 2020-08-20",
hoverinfo = "text",
text = "Classes Begin",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_segments(x = as.Date("2020-10-03"),
xend = as.Date("2020-10-03"),
y = ~min(both_yminc), yend = ~max(both_ymaxc),
opacity = 0.35,
name = "</br> First Home Football Game",
"</br> 2020-10-03",
hoverinfo = "text",
text = "First Home Football Game",
showlegend = FALSE,
line = list(color = "black", dash = "dash")) %>%
plotly::add_markers(x = ~date, y = ~log_total_copies_both,
data = wrfc_both,
hoverinfo = "text",
showlegend = FALSE,
text = ~paste('</br> Date: ', date,
'</br> Actual Log Copies: ', round(log_total_copies_both, digits = 2)),
marker = list(color = '#E7298A', size = 6, opacity = 0.65))
p_wrf_c
save(p_wrf_c, file = "./plotly_objs/p_wrf_c.rda")
save(wrfa_both, file = "./plotly_objs/wrfa_both.rda")
save(wrfb_both, file = "./plotly_objs/wrfb_both.rda")
save(wrfc_both, file = "./plotly_objs/wrfc_both.rda")
save(date_vec_botha, file = "./plotly_objs/date_vec_botha.rda")
save(date_vec_bothb, file = "./plotly_objs/date_vec_bothb.rda")
save(date_vec_bothc, file = "./plotly_objs/date_vec_bothc.rda")
save(both_ymina, file = "./plotly_objs/both_ymina.rda")
save(both_ymaxa, file = "./plotly_objs/both_ymaxa.rda")
save(both_yminb, file = "./plotly_objs/both_yminb.rda")
save(both_ymaxb, file = "./plotly_objs/both_ymaxb.rda")
save(both_yminc, file = "./plotly_objs/both_yminc.rda")
save(both_ymaxc, file = "./plotly_objs/both_ymaxc.rda")